package com.tjec.project.businessWorkbench.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tjec.framework.interceptor.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.LoginUser;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.businessWorkbench.domain.BmsBusinessPickup;
import com.tjec.project.businessWorkbench.domain.BmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.domain.ImportBmsBusinessTask;
import com.tjec.project.businessWorkbench.domain.ImportBmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.service.IBmsImportExcelService;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocCatalogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.BusinessExcelUtil;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 项目文档目录Controller
 * 
 * @author pangyognfeng
 * @date 2020-04-26
 */
@Api(value = "项目文档目录", tags = { "项目文档目录接口" })
@RestController
@RequestMapping("/businessWorkbench/catalog")
public class BmsProjectDocCatalogController extends BaseController {
	@Autowired
	private IBmsProjectDocCatalogService bmsProjectDocCatalogService;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private IBmsImportExcelService bmsImportExcelService;

	/**
	 * 查询项目文档目录列表
	 */
	@ApiOperation("查询项目文档目录列表")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:list')")
	@GetMapping("/list")
	public TableDataInfo list(BmsProjectDocCatalog bmsProjectDocCatalog) {
		startPage();
		List<BmsProjectDocCatalog> list = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogList(bmsProjectDocCatalog);
		return getDataTable(list);
	}

	/**
	 * 导出项目文档目录列表
	 */
	@ApiOperation("导出项目文档目录列表")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:export')")
	@Log(title = "项目文档目录", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BmsProjectDocCatalog bmsProjectDocCatalog) {
		List<BmsProjectDocCatalog> list = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogExportList(bmsProjectDocCatalog);
		BusinessExcelUtil<BmsProjectDocCatalog> util = new BusinessExcelUtil<BmsProjectDocCatalog>(
				BmsProjectDocCatalog.class);
		return util.exportExcel(list, "catalog");
	}

	/**
	 * 获取项目文档目录详细信息
	 */
	@ApiOperation("获取项目文档目录详细信息")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:query')")
	@GetMapping(value = "/{guid}")
	public AjaxResult getInfo(@PathVariable("guid") String guid) {
		return AjaxResult.success(bmsProjectDocCatalogService.selectBmsProjectDocCatalogByGuid(guid));
	}

	/**
	 * 新增项目文档目录
	 */
	@ApiOperation("新增项目文档目录")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:add')")
	@Log(title = "项目文档目录", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BmsProjectDocCatalog bmsProjectDocCatalog) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		bmsProjectDocCatalog.setCreateBy(loginUser.getUser().getUserId());
		return AjaxResult.success(bmsProjectDocCatalogService.insertBmsProjectDocCatalogNew(bmsProjectDocCatalog));
	}

	/**
	 * 修改项目文档目录
	 */
	@ApiOperation("修改项目文档目录")
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:edit')")
	@Log(title = "项目文档目录", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BmsProjectDocCatalog bmsProjectDocCatalog) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		bmsProjectDocCatalog.setUpdateBy(loginUser.getUser().getUserId());
		return toAjax(bmsProjectDocCatalogService.updateBmsProjectDocCatalog(bmsProjectDocCatalog));
	}

	/**
	 * 获取项目文档目录下拉树列表
	 */
	@ApiOperation("获取项目文档目录下拉树列表")
	@GetMapping("/treeselect")
	public AjaxResult treeselect(BmsProjectDocCatalog bmsProjectDocCatalog) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("businessId", bmsProjectDocCatalog.getBusinessId());
		List<BmsProjectDocCatalog> bmsProjectDocCatalogs = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogList(bmsProjectDocCatalog);
		if (bmsProjectDocCatalogs != null && bmsProjectDocCatalogs.size() > 0) {
			bmsProjectDocCatalogs.forEach(item -> {
				if("0".equals(item.getIsEdit())) {
					Integer res = bmsProjectDocCatalogService.selectPickUpSimpleCounts(map.get("businessId"));
					item.setCounts(res == null ? 0 : res);
				}
				if("2".equals(item.getIsEdit())) {
					Integer res = bmsProjectDocCatalogService.selectArchiveSimpleCounts(map.get("businessId"));
					item.setCounts(res == null ? 0 : res);
				}
			});
		}
		return AjaxResult.success(bmsProjectDocCatalogService.buildDocCatalogTreeSelect(bmsProjectDocCatalogs));
	}
	
	/**
	 * 获取项目文档目录下拉树列表
	 */
	@ApiOperation("优化版-获取项目文档目录下拉树列表")
	@GetMapping("/treeselectNew")
	public AjaxResult treeselectNew(BmsProjectDocCatalog bmsProjectDocCatalog) {
		List<BmsProjectDocCatalog> bmsProjectDocCatalogs = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogListNew(bmsProjectDocCatalog);
		return AjaxResult.success(bmsProjectDocCatalogService.buildDocCatalogTreeSelect(bmsProjectDocCatalogs));
	}
	
	/**
	 * 获取项目文档目录下拉树列表
	 */
	@ApiOperation("优化版-根目录-获取项目文档目录下拉树列表")
	@GetMapping("/treeselectFirstNew")
	public AjaxResult treeselectFirstNew(BmsProjectDocCatalog bmsProjectDocCatalog) {
		List<BmsProjectDocCatalog> bmsProjectDocCatalogs = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogListFirstNew(bmsProjectDocCatalog);
		return AjaxResult.success(bmsProjectDocCatalogService.buildDocCatalogTreeSelect(bmsProjectDocCatalogs));
	}
	
	/**
	 * 获取项目文档目录下拉树列表
	 */
	@ApiOperation("获取项目文档目录下拉树列表")
	@GetMapping("/treeselect1")
	public AjaxResult treeselect1(BmsProjectDocCatalog bmsProjectDocCatalog) {
		List<BmsProjectDocCatalog> bmsProjectDocCatalogs = bmsProjectDocCatalogService
				.selectBmsProjectDocCatalogList(bmsProjectDocCatalog);
		return AjaxResult.success(bmsProjectDocCatalogService.buildDocCatalogTreeSelect(bmsProjectDocCatalogs));
	}

	/**
	 * 删除项目文档目录
	 */
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:remove')")
	@Log(title = "项目文档目录", businessType = BusinessType.DELETE)
	@ApiOperation("删除项目文档目录")
	@DeleteMapping("/{guid}")
	public AjaxResult remove(@PathVariable String guid) {
		return AjaxResult.success(bmsProjectDocCatalogService.deleteBmsProjectDocCatalogByGuid(guid));
	}
	
	/**
	 * 删除项目文档目录-校验
	 */
	// @PreAuthorize("@ss.hasPermi('businessWorkbench:catalog:remove')")
	@Log(title = "项目文档目录", businessType = BusinessType.DELETE)
	@ApiOperation("删除项目文档目录")
	@DeleteMapping("/removeCheck/{guid}")
	public AjaxResult removeCheck(@PathVariable String guid) {
		return AjaxResult.success(bmsProjectDocCatalogService.removeCheck(guid));
	}

	@ApiOperation("项目文档目录导入清单")
	@Log(title = "导入项目文档目录清单", businessType = BusinessType.IMPORT)
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, String businessId) throws Exception {
		BusinessExcelUtil<BmsProjectDocCatalog> util = new BusinessExcelUtil<BmsProjectDocCatalog>(
				BmsProjectDocCatalog.class);
		List<BmsProjectDocCatalog> projectDocCatalogList = null;
		try {
			projectDocCatalogList = util.importExcel(file.getInputStream());
		} catch (Exception e) {
			return AjaxResult.error("请导入本系统导出的自制模板文件！", 100);
		}

		if (projectDocCatalogList == null) {
			return AjaxResult.error("请导入本系统导出的自制模板文件！", 100);
		} else if (projectDocCatalogList.size() == 0) {
			return AjaxResult.error("导入文件为空文件！", 100);
		} else {
			for (BmsProjectDocCatalog bmsProjectDocCatalog : projectDocCatalogList) {
				bmsProjectDocCatalog
						.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
				bmsProjectDocCatalog.setBusinessId(businessId);
			}
			int res = bmsProjectDocCatalogService.importBmsProjectDocCatalogsList(projectDocCatalogList);
			return AjaxResult.success(res);
		}
	}

	/**
	 * 导入项目文档目录模板
	 * 
	 * @throws Exception
	 */
	@ApiOperation("导入项目文档目录模板")
	@GetMapping("/{guid}/{businessId}")
	public AjaxResult importDocCatalogTemplate(@PathVariable("guid") String guid,
			@PathVariable("businessId") String businessId) throws Exception {
		return toAjax(bmsImportExcelService.importTemplte(guid, "3", businessId));
	}
}
